package cn.wenhe9.gulimall.product.service.impl;

import cn.wenhe9.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.wenhe9.gulimall.product.dao.BrandDao;
import cn.wenhe9.gulimall.product.entity.BrandEntity;
import cn.wenhe9.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public IPage<BrandEntity> queryPage(Map<String, Object> params) {
        Long currnet = (Long) params.get("page");
        Long limit = (Long) params.get("limit");
        IPage<BrandEntity> page = this.page(
                new Page<BrandEntity>(currnet, limit),
                new QueryWrapper<BrandEntity>()
        );
        return page;
    }

    @Override
    public Page<BrandEntity> getBrandByPage(Long page, Long limit, String key) {
        Page<BrandEntity> entityPage = this.page(new Page<BrandEntity>(page, limit), new QueryWrapper<BrandEntity>().like("name", key));
        return entityPage;
    }

    @Override
    public void batchDeleteByIds(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        //保证冗余字段的数据一致
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())){
            //同步更新其他表中的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());

            //TODO 更新其他关联
        }
    }

}